package calao.storage.bdb;

import java.util.HashSet;
import java.util.Arrays;

import calao.storage.Record;
import calao.storage.Index;
import static calao.storage.DirectAccess.*;

class KeySet
extends HashSet<Object>
{

	static class TupleKey 
	{
		Object[] tuple;
		TupleKey(Object[] tuple)
		{
			this.tuple = tuple;
		}
		@Override
		public int hashCode()
		{
			return Arrays.hashCode(tuple);
		}
		@Override
		public boolean equals(Object obj)
		{
			try {
				return Arrays.equals(tuple, (Object[])obj);
			}
			catch (ClassCastException e) {
				return false;
			}
		}
	}

	final Index index;

	KeySet(TableDatabase tableDB)
	{
		this.index = tableDB.primaryIndex;
	}

	boolean addKey(Record record)
	{
		return add(index.getFieldCount()==1? get(record, index.getFirstField())
				: new TupleKey(record.getPrimaryKey()));
	}

}
